package com.codeduck.service.producer;

import com.alibaba.fastjson.JSON;
import com.codeduck.kafka.KafkaInvoker;
import com.codeduck.pojo.SystemLog;
import com.codeduck.pojo.TopicHandler;
import com.codeduck.utils.MockUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @auther Mahone Yan
 * @date 2023/7/16
 */
@Component
public class SystemLogProducer {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemLogProducer.class);

    @Resource
    private KafkaInvoker kafkaInvoker;

    @Scheduled(cron = "0/1 * * * * ?")
    public void job() {
        this.doJob();
    }

    @Scheduled(cron = "0/1 * * * * ?")
    public void job1() {
        this.doJob();
    }

    private void doJob() {
        try {
            kafkaInvoker.send(TopicHandler.SYSTEM_LOG_TOPIC.getTopic(), JSON.toJSONString(MockUtil.getJavaBean(SystemLog.class)));
            LOGGER.info("============> 系统日志已发送至 Topic[{}] <============", TopicHandler.SYSTEM_LOG_TOPIC.getTopic());
        } catch (Exception e) {
            LOGGER.error("定时任务向Kafka发送消息时出现异常", e);
        }
    }
}
